const {
    pack,
    rename,
    removePath,
    createPath,
    copy,
    readFile,
    writeFile,
    moveFile,
} = require("./lib");
const path = require("path");
const { resolve}=path
//锁屏目录
const allfilePath = process.argv[2].replace('"', "")

const packFlow = async function (allfilePath) {
    let TIME_FILE = parseInt((await readFile(resolve(__dirname, "./TIME"))))
    console.log(`这是你第${TIME_FILE+1}次打包,目录为:`,allfilePath)
    //锁屏同级目录
    const filePathRoot = path.resolve(allfilePath, "../");
    const createTime = String(Number(new Date()));
    //创建缓存目录
    const tmpPath = path.resolve(filePathRoot, `./tmp/`);
    // await createPath(tmpPath);
    // 拷贝所有文件到缓存目录
    await copy(allfilePath)(tmpPath)
    //0 重命名锁屏
    // await rename(resolve(tmpPath, "./lockscreen/lockscreen/1618214834988/"), resolve(tmpPath, `./lockscreen/lockscreen/${createTime}/`),)
    // 1 压缩1618214834988.zip 到缓存目录
    const descriptionXml = resolve(tmpPath, "./lockscreen/description.xml");
    let xml = (await readFile(descriptionXml)).replace(/\d{13}/gi, createTime);
    await writeFile(descriptionXml)(xml);
    await pack(resolve(tmpPath, "./lockscreen/lockscreen/1618214834988/"))(resolve(tmpPath, `./lockscreen/lockscreen/${createTime}.zip`));
    // 2 删除 1618214834988 文件夹
    await removePath(resolve(tmpPath, "./lockscreen/lockscreen/1618214834988/"));

    // 3 压缩 / lockscreen为 /lockscreen.itz
    await pack(resolve(tmpPath, "./lockscreen/"))(resolve(tmpPath, "./lockscreen.itz"));
    // 4 删除 /lockscreen 文件夹
    await removePath(resolve(tmpPath, "./lockscreen/"));
    // 5 压缩 lockscreen.itz为lockscreen
    await pack(resolve(tmpPath, "./lockscreen.itz"))(resolve(tmpPath, "./lockscreen"));
    await removePath(resolve(tmpPath, "./lockscreen.itz"));

    //3 压缩 /锁屏 为 theme.itz 
    await pack(resolve(tmpPath, "./"))(resolve(filePathRoot, `./${TIME_FILE+1+''}.itz`));
    await removePath(resolve(tmpPath, "./"));
    await writeFile(resolve(__dirname, "./TIME"))(TIME_FILE + 1+'')
};
packFlow(allfilePath)